#encoding=utf-8
'''
Created on 2013-2-20

@author: Administrator
'''
from actionModule.handle import base
from base.common import createHtml, retSuccess
from config.settings import STATIC_URL, PROJECT_PATH
from config.db.models import ProParam, Html
import json
import os
formJson = [
      {'id':'id', 'key':'Hidden', 'name':'主键'},
      {'id':'name', 'key':'Input', 'name':'名称', 'noNull':True},
      {'id':'url', 'key':'Input', 'name':'数据库同步链接', 'noNull':True},
      {'id':'path', 'key':'Input', 'name':'路径', 'noNull':True},
      {'id':'type', 'key':'Select', 'name':'类型', 'subs':[{'id':'java','text':'java'},{'id':'python','text':'python'}],'noNull':True},
      {'key':'Submit', 'action':'project.create','back':'closeTab'},
]
paramJson=[
      {'id':'id', 'key':'Hidden', 'name':'主键'},
      {'id':'webRoot', 'key':'Input', 'name':'根目录'},
      {'id':'pro_id', 'key':'Hidden', 'name':'关联项目'},
      {'id':'request_common', 'key':'Input', 'name':'通用bean', 'noNull':True},
      {'id':'jndiName', 'key':'Input', 'name':'连接池', 'noNull':True},
      {'id':'login','key':'Input', 'name':'登陆页面', 'noNull':True},
      {'key':'Submit','action':'proParam.saveParam','other':"<button class='btn' callFunc='click:createFile'>生成文件</button>",'noescape':True},     
]

jsPath=STATIC_URL+'/js/base'
cssPath=STATIC_URL+'/css'
htmlPath=os.path.join(PROJECT_PATH,'yqy')+'/form/html'
jsonPath=os.path.join(PROJECT_PATH,'yqy')+'/form'
importJs=os.path.join(PROJECT_PATH,'yqy')+'/form/js'
class project(base):
    
    def __init__(self):
        self.formJson=formJson
        
    def qryParamDiv(self):
        return createHtml(paramJson)
    
    def createFile(self):
        proId=self.getPost("pro_id")
        item=self.table.objects.get(id=proId)
        #文件路径
        path=item['path']+"/src/app.properties"
        try:
            objFile=open(path,'wb')
            content=[]
            for param in self.request.params:
                if param=='id' or param=='pro_id':continue
                else:
                    content.append(param)
                    content.append("=")
                    content.append(self.request.params[param])
                    content.append("\n")
            objFile.write("".join(content))
            objFile.close()
            return retSuccess("生成文件成功:"+path)        
        except Exception:
            return retSuccess("生成文件失败:"+path)       
            
    def syncJs(self):
        item=self.table.objects.get(id=self.getPost("id"))
        param=json.loads(ProParam.objects.get(pro_id=self.getPost("id"))['content'])
        
        objPath="%s/%s/js"%(item['path'],param['webRoot'])
        mkdir(objPath)
        copy(jsPath,objPath)
        print("同步脚本成功")
        objPath="%s/%s/css"%(item['path'],param['webRoot'])
        mkdir(objPath)
        copy(cssPath,objPath)
        
        objPath+="/less"
        mkdir(objPath)
        copy(cssPath+"/less",objPath)
        
        return retSuccess("同步脚本成功")
        
    def syncHtml(self):
        item=Html.objects.get(id=self.getPost("id"))
        pro=self.table.objects.get(id=self.getPost("proId"))
        param=json.loads(ProParam.objects.get(pro_id=self.getPost("proId"))['content']) 
        objPath="%s/%s%s"%(pro['path'],param['webRoot'],item['path'])
        mkdir(objPath)
        copy(htmlPath+"/%s.html"%item['name'],objPath)
        htmlRet=objPath+"/%s.html"%item['name']
        #同步json表单
        objPath="%s/%s"%(pro['path'],"src/json")
        mkdir(objPath)
        copy(jsonPath+"/%s.valid"%item['formId'],objPath)
        #同步js文件 同时打印表单验证信息
        objPath="%s/%s%s/js"%(pro['path'],param['webRoot'],item['path'])
        mkdir(objPath)
        copy(importJs+"/%s.js"%(item['name']+"_temp"),objPath)
        
        objPath="%s/%s/valid"%(pro['path'],param['webRoot'])
        mkdir(objPath)
        copy(importJs+"/%s_valid.js"%(item['name']),objPath)
        return retSuccess("同步成功") 
        
    def syncHtmlJs(self):
        item=Html.objects.get(id=self.getPost("id"))
        pro=self.table.objects.get(id=self.getPost("proId"))
        param=json.loads(ProParam.objects.get(pro_id=self.getPost("proId"))['content']) 
        objPath="%s/%s%s/js"%(pro['path'],param['webRoot'],item['path'])
        mkdir(objPath)
        copy(importJs+"/%s.js"%(item['name']+"_temp"),objPath)
        
        return retSuccess("同步js成功") 
def copy(s,o):
    dirCmd="copy %s %s"%(s.replace("/", "\\"),o.replace("/", "\\"))
    os.system(dirCmd) 
     
def mkdir(s):
    dirCmd="mkdir %s"%s.replace("/", "\\")
    os.system(dirCmd) 
        